package org.eliza.rpc.rpc.model

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.rpc.RemoteService
import kotlinx.rpc.annotations.Rpc
import kotlin.coroutines.CoroutineContext

@Rpc
interface AwesomeService : RemoteService {
    suspend fun getNews(city: String): Flow<String>

    suspend fun daysUntilStableRelease(): Int
}

data class AwesomeParameters(val stable: Boolean, val daysUntilStable: Int?)

class AwesomeServiceImpl(
    val parameters: AwesomeParameters,
    override val coroutineContext: CoroutineContext,
) : AwesomeService {
    override suspend fun getNews(city: String): Flow<String> {
        return flow {
            emit("Today is 23 degrees!")
            emit("Harry Potter is in $city!")
            emit("New dogs cafe has opened doors to all fluffy customers!")
        }
    }

    override suspend fun daysUntilStableRelease(): Int {
        return if (parameters.stable) 0 else {
            parameters.daysUntilStable ?: error("Who says it will be stable?")
        }
    }
}